package com.learn.rabbit.base.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.learn.rabbit.base.dto.Info;
import com.learn.rabbit.base.dto.Message;
import com.learn.rabbit.base.sender.*;
import com.learn.rabbit.base.util.JacksonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * 测试接口
 *
 * @author zhousiqi
 * @version 1.0.0
 * @date 2019/2/26 18:18
 */
@RestController()
@RequestMapping(SendController.PATH)
@Slf4j
public class SendController {
    static final String PATH = "/send";

    @Resource
    private DirectFirstSender directFirstSender;

    @Resource
    private DirectSecondSender secondDirectSender;

    @Resource
    private FanoutFirstSender fanoutFirstSender;

    @Resource
    private FanoutSecondSender fanoutSecondSender;

    @Resource
    private TopicFirstSender topicFirstSender;

    @Resource
    private TopicSecondSender topicSecondSender;

    @Resource
    private JacksonUtil jacksonUtil;

    @GetMapping(value = "/direct/{message}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String directFirstSend(@PathVariable String message) {
        String uuid = UUID.randomUUID().toString();
        directFirstSender.send(uuid, message);
        return uuid;
    }

    @GetMapping(value = "/direct2/{message}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String secondDirectSend(@PathVariable String message) {
        String uuid = UUID.randomUUID().toString();
        secondDirectSender.send(uuid, message);
        return uuid;
    }

    @GetMapping(value = "/fanout", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String fanoutFirstSend() {
        String uuid = UUID.randomUUID().toString();
        fanoutFirstSender.send(uuid, getInfo());
        return uuid;
    }

    @GetMapping(value = "/fanout2", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String fanoutSecondSend() {
        String uuid = UUID.randomUUID().toString();
        fanoutSecondSender.send(uuid, jacksonUtil.writeValueAsString(getInfo()));
        return uuid;
    }

    @GetMapping(value = "/topic", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String topicFirstSend() {
        String uuid = UUID.randomUUID().toString();
        topicFirstSender.send(uuid, getMessage());
        return uuid;
    }

    @GetMapping(value = "/topic2", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String topicSecondSend() {
        String uuid = UUID.randomUUID().toString();
        topicSecondSender.send(uuid, jacksonUtil.writeValueAsString(getMessage()));
        return uuid;
    }

    private Info getInfo() {
        Info info = new Info();
        info.setName("test");
        info.setAbility("none");
        return info;
    }

    private Message<Info> getMessage() {
        Message<Info> message = new Message<>();
        message.setCode("101");
        message.setMessage("test message");
        message.setBody(getInfo());
        return message;
    }

}
